/*
 * Copyright (C) 2014 hu
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.sentiment.webcollector.generator;

import org.bson.types.ObjectId;

import com.sentiment.config.ConfigDb;
import com.sentiment.database.bean.Datum;
import com.sentiment.database.codec.DatumCodec;
import com.sentiment.database.dbUtil.DbCodecDriver;
import com.sentiment.webcollector.util.Config;

/**
 *
 * @author hu
 */
public class StandardGenerator implements Generator {

	protected int totalGenerate = 0;
	protected Integer topN = null;
	protected int maxRetry = Config.MAX_RETRY;
	protected DbCodecDriver<Datum> driver;
	protected ObjectId mark;

	public StandardGenerator(DbCodecDriver<Datum> driver) {
		this.driver = driver;
		totalGenerate = 0;
	}

	public void close() {
		driver.closeConnection();
	}

	@Override
	public Datum next() {
		if (topN != null) {
			if (totalGenerate >= topN) {
				return null;
			}
		}
		while (true) {
			// 获取下一条待爬取数据
			if (!CRAWLQUEUE.isEmpty()) {

				Datum datum = CRAWLQUEUE.poll();
				if (datum.getStatus() == Datum.STATUS_DB_FETCHED) {
					continue;
				} else {
					if (datum.getRetry() >= maxRetry) {
						continue;
					}
					totalGenerate++;
					return datum;
				}

			} else {
				return null;
			}
		}
	}

	public int getTotalGenerate() {
		return totalGenerate;
	}

	public Integer getTopN() {
		return topN;
	}

	public void setTopN(Integer topN) {
		this.topN = topN;
	}

	public int getMaxRetry() {
		return maxRetry;
	}

	public void setMaxRetry(int maxRetry) {
		this.maxRetry = maxRetry;
	}

}
